addLoadEvent(function() {

	$("#post-btn").click(function() {
		$("#modal-post").modal({
			backdrop : true,
			show : true
		});
	});

	$("#post-btn").tooltip({
		animation : true,
		title : "Click to post something!",
		placement : "bottom"
	});

	$("#btn-up-image").tooltip({
		animation : true,
		title : "image less than 500kb and 1024x768 allowed",
		placement : "right"
	});

});

var post_type = "text";
var link_content = -1;
var image_content = -1;

function postBlog(type) {
	$("#modal-post").modal({
		backdrop : true,
		show : true
	});
	$('#btn-'+type.toLowerCase()).click();
}

function change_post_type(type) {
	post_type = type.toLowerCase();
	$("#type-span").html(type);
	$(".post-area").hide('fast');
	$("#post-area-" + post_type).show('fast');
}

function post() {
	post_title = "";
	post_content = "";
	tgas_str = tags.join(' ');
	if (post_type == 'text') {
		post_content = $("#input-text-content")[0].value;
		post_title = $("#input-text-title")[0].value;
	} else if (post_type == 'picture') {
		post_content = image_content;
		post_title = $("#input-image-escription")[0].value;
	} else if (post_type == 'link') {
		post_content = link_content;
		post_title = $("#input-link-escription")[0].value;
	} else if (post_type == 'music') {

	} else if (post_type == 'vedio') {

	} else {
		return;
	}

	if (post_content) {
		$.post('/MyMicroBlog/index.php/feed/post', {
			type : post_type,
			content : post_content,
			title : post_title,
			tags : tgas_str
		}, function(responseText, textStatus, XMLHttpRequest) {
			$("#modal-post").modal("hide");
			// window.location.reload();
			reloadMain();
		});
	}
}

function upload_image() {
	var file = $('#image-up')[0].files[0];
	var data = new FormData();
	data.append('image-up', file);
	$
			.ajax({
				url : '/MyMicroBlog/index.php/feed/upload_image',
				type : 'POST',
				data : data,
				cache : false,
				contentType : false, // 不可缺
				processData : false, // 不可缺
				success : function(data) {
					var result = eval('(' + data + ')');
					if (result.state > 0) {
						$('#post-image-hint').hide();
						image_content = result.message;
						var html = '<img style = "height:200px" src = "/MyMicroBlog/public/image/feed/'
								+ image_content + '" </img>';
						$('#btn-up-image').html(html);
						$('#btn-up-image').removeClass('btn');
					} else {
						$('#btn-up-image').addClass('btn');
						$('#btn-up-image').html('choose picture');
						$('#post-image-hint').html(result.message);
						$('#post-image-hint').show();
						$('#post-image-hint').addClass('pulse');
					}
				}
			});
}

function get_link_title() {
	link = $('#input-link-url')[0].value;
	if (!link)
		return;
	$.get('/MyMicroBlog/index.php/feed/url_to_link?url=' + link,
			function(data) {
				link_content = data;
				if (data != -1) {
					$('#link-ctn').html(data);
				} else {
					$('#link-ctn').html('The url is unreachable');
				}
				$('#link-ctn').show();
				$("#input-link-url").hide();
			});
}